<script setup lang='ts'>
import { getClassAll, type IClass } from '@/api/classApi';
import { getStudentAll, type IStudent } from '@/api/studentApi';
import { getGroup } from '@/api/userApi';
import { ref } from 'vue';
import { ElMessage } from 'element-plus';


defineOptions({
  name: 'VStudentGroup',
})


const classList = ref<IClass[]>([])
async function fetchClassAll() {
  const result = await getClassAll()
  classList.value = result.data as IClass[]
}

fetchClassAll()

const studentList = ref<IStudent[]>([])
async function fetchStudentAll(class_id: number) {
  const result = await getStudentAll(class_id, 1, 1000)
  studentList.value = result.data.list
}

const dialogVisible = ref(false)

// 查找班级学生
const searchClassId = ref(null)
const handleSelectChange = function () {
  fetchStudentAll(searchClassId.value!)
}

const studentGroupModel = ref(null)
// 分组策略

const groupData = ref([])
async function handleGroup() {
  const { code, msg, data } = await getGroup(studentGroupModel.value, searchClassId.value)
  if (code === 2000) {
    ElMessage.success(msg)
    groupData.value = data
  }
}


</script>

<template>
  <div class='container'>
    <div class="operator">
      <el-select @change="handleSelectChange" v-model="searchClassId" placeholder="选择班级" size="large"
        style="width: 240px">
        <el-option v-for="item in classList" :key="item.id" :label="item.nickname" :value="item.id" />
      </el-select>
      <!-- 选择分组 -->
      <el-select v-if="searchClassId" @change="handleGroup" v-model="studentGroupModel" placeholder="选择分组策略"
        size="large" style="width: 240px">
        <el-option :key="1" label="随机分组" :value="1" />
        <el-option :key="2" label="性别优先分组" :value="2" />
      </el-select>
    </div>
    <div class="student-group-wrap">
      <div class="student-list">
        <el-table :data="studentList" style="width: 100%">
          <el-table-column label="序号" type="index" width="80" />
          <el-table-column prop="account" label="账号" width="180" />
          <el-table-column prop="nickname" label="学生名称" width="180" />
        </el-table>
      </div>
      <!-- 分组结果 -->
      <div class="student-group-list">
        <div v-for="groupInfo in groupData">
          <div>{{ groupInfo.groupName }}</div>
          <div>
            <el-table :data="groupInfo.members" style="width: 100%">
              <el-table-column label="序号" type="index" width="80" />
              <el-table-column prop="account" label="账号" width="180" />
              <el-table-column prop="nickname" label="学生名称" width="180" />
            </el-table>
          </div>
        </div>
      </div>
    </div>


  </div>
</template>

<style scoped lang='scss'>
.student-group-wrap {
  display: flex;
}

.student-group-list {
  margin-left: 120px;
}
</style>